package listbyorder.access001_100.test34;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/5/30 15:19
 */
public class Solution1 {

    // 典型的二分查找算法
    public int[] searchRange(int[] nums, int target) {
        if (nums == null || nums.length == 0) return new int[]{-1, -1};
        int s = 0;
        int e = nums.length - 1;
        while (s < e) {
            int mid = s + ((e - s) >> 1);
            if (nums[mid] < target) {
                s = mid + 1;
            } else {
                e = mid;
            }
        }
        if (nums[s] != target) {
            return new int[]{-1, -1};
        }
        int l = s;
        s = 0;
        e = nums.length - 1;
        while (s < e) {
            int mid = s + ((e - s + 1) >> 1);
            if (nums[mid] > target) {
                e = mid - 1;
            } else {
                s = mid;
            }
        }
        return new int[]{l, s};
    }
}
